//
//  HOTBBSViewController.m
//  Genpara
//
//  Created by AndyChang.zhang on 14-9-5.
//  Copyright (c) 2014年 zhangchh. All rights reserved.
//

#import "HOTDumpSearchViewController.h"
#import "ZZZUIScrollView+Extends.h"
#import "HOTDumpSearchResultViewController.h"
#import "HOTAddGenbaViewController.h"

@interface HOTDumpSearchViewController () <UITextFieldDelegate> {
    
    __unsafe_unretained HOTDumpSearchViewController *unsafeObj;
    
}

@property(nonatomic,copy)NSString *dist_id;
@property(nonatomic,copy)NSString *workp_id;
@property(nonatomic,copy)NSString *company_id;
@property(nonatomic,copy)NSString *free_word;
@property(nonatomic,strong)NSMutableArray *workpModelList;
@property(nonatomic,strong)NSMutableArray *compenyModelList;
@end

@implementation HOTDumpSearchViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    [self setNaviTitle:@"現場掲示板"];
    
    unsafeObj = self;
    
    self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"bg_dump"]];
    
    [self initView];
    
    self.compenyModelList = [NSMutableArray array];
    self.workpModelList = [NSMutableArray array];
    
    [self showLoading];
    [self requestCompanyDataFromNet];
}

-(void)initView {
    
    [_mainScrollView setFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
    [_mainScrollView setContentSize:CGSizeMake(SCREEN_WIDTH, CGRectGetMaxY(_addSpaceNameBtn.frame)+30)];
    
    NSArray *modelList = [HOTUtils getPullModelListFromPlist];
    _regionPullDownView.pullDownModelList = modelList;
    _regionPullDownView.parentView = _mainScrollView;
    
    __unsafe_unretained HOTDumpSearchViewController *tempObj = self;
    _regionPullDownView.SelectedComplete = ^(ZZZPullDownModel *model) {
        
        [tempObj.compenyModelList removeAllObjects];
        unsafeObj.dist_id = model.value;
        [unsafeObj requestCompanyDataFromNet];
    };
    [_regionPullDownView setSelectedModel:[modelList firstObject]];
    self.dist_id = ((ZZZPullDownModel *)[modelList firstObject]).value;
    
    _compenyPullDownView.parentView = _mainScrollView;
    _workPlacePullDownView.parentView = _mainScrollView;
}
/**
 *  获取公司列表
 */
-(void)requestCompanyDataFromNet {
    
    NSDictionary *parameters = @{REQ_KEY_USER_TOKEN:self.userInfo.token,
                                 REQ_KEY_CITY_ID:[HOTUtils getString:self.dist_id],
                                 };
    [self requestWithURL:HOT_URL_COMPANY_LIST Parameters:parameters success:^(NSInteger result, NSArray * dataArr) {
        
        //[unsafeObj loadingSuccess];
        //空值
        ZZZPullDownModel *model = [[ZZZPullDownModel alloc] init];
        model.value = @"";
        model.titleStr = @"指定しない";
        
        [self.compenyModelList addObject:model];
        for (NSDictionary *dic in dataArr) {
            
            ZZZPullDownModel *model = [[ZZZPullDownModel alloc] init];
            model.value = [dic objectForKey:@"company_id"];
            model.titleStr = [dic objectForKey:@"company_name"];
            
            [self.compenyModelList addObject:model];
        }
        _compenyPullDownView.SelectedComplete = ^(ZZZPullDownModel *model) {
            
            unsafeObj.company_id = model.value;
            //获取现场列表数据
            [unsafeObj requestDataFromNet];
        };
        _compenyPullDownView.pullDownModelList = self.compenyModelList;
        [_compenyPullDownView setSelectedModel:[self.compenyModelList firstObject]];
        
        //获取现场列表数据
        //[self requestDataFromNet];
        
    } failure:^(NSError *error, NSString *errMsg) {
        
        [unsafeObj loadingSuccess];
        if ([unsafeObj checkErrResult:error errMsg:errMsg]) {
            return ;
        }
        if (errMsg.length > 0) {
            
            ZZZPopupView *popupView = [[ZZZPopupView alloc] initWithTitle:ALERT_TITLE
                                                                  message:errMsg
                                                                 delegate:nil
                                                        cancelButtonTitle:nil
                                                         otherButtonTitle:ALERT_BTN_OK];
            
            [popupView showInWindow];
        }
    }];
    
}

-(void)requestDataFromNet {
    
//    if ([self.company_id isEqualToString:@""] || [self.dist_id isEqualToString:@""]) {
//        
//        return;
//    }
    
    [self showLoading];
    
    NSDictionary *parameters = @{REQ_KEY_USER_TOKEN:self.userInfo.token,
                                 REQ_KEY_USER_ID:self.userInfo.userId,
                                 REQ_KEY_DIST_ID:self.dist_id,
                                 REQ_KEY_COMPENY_ID:self.company_id,
                                 REQ_KEY_WORKP_ID:self.workp_id,
                                 REQ_KEY_FREE_WORD:self.free_word};
    
    //__unsafe_unretained HOTCloseDownDetailListViewController *tempObj = self;
    [self requestWithURL:HOT_URL_MOCK_MOCK Parameters:parameters success:^(NSInteger result, NSArray * dataArr) {
        
        [unsafeObj loadingSuccess];
        [self.workpModelList removeAllObjects];
        //空值
        ZZZPullDownModel *model = [[ZZZPullDownModel alloc] init];
        model.value = @"";
        model.titleStr = @"指定しない";
        [self.workpModelList addObject:model];
        for (NSDictionary *dic in dataArr) {
            
            ZZZPullDownModel *model = [[ZZZPullDownModel alloc] init];
            model.value = [dic objectForKey:@"workp_id"];
            model.titleStr = [dic objectForKey:@"workp_name"];
            
            [self.workpModelList addObject:model];
        }
        _workPlacePullDownView.SelectedComplete = ^(ZZZPullDownModel *model) {
            
            unsafeObj.workp_id = model.value;
        };
        _workPlacePullDownView.pullDownModelList = self.workpModelList;
        [_workPlacePullDownView setSelectedModel:[self.workpModelList firstObject]];
        
    } failure:^(NSError *error, NSString *errMsg) {
        
        [unsafeObj loadingSuccess];
        if ([unsafeObj checkErrResult:error errMsg:errMsg]) {
            return ;
        }
        if (errMsg.length > 0) {
            
            ZZZPopupView *popupView = [[ZZZPopupView alloc] initWithTitle:ALERT_TITLE
                                                                  message:errMsg
                                                                 delegate:nil
                                                        cancelButtonTitle:nil
                                                         otherButtonTitle:ALERT_BTN_OK];
            
            [popupView showInWindow];
        }
    }];
}

-(BOOL)textFieldShouldReturn:(UITextField *)textField {
    
    [textField resignFirstResponder];
    [_mainScrollView setContentOffset:CGPointMake(0, 0) animated:YES];
    
    self.free_word = textField.text;
    
    //检索现场名
    [self requestDataFromNet];
    
    return YES;
}

-(void)textFieldDidBeginEditing:(UITextField *)textField {
    
    [_mainScrollView scrollViewToVisible:textField animated:YES];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)searchBtnClicked:(id)sender {
    
//    if (!_workPlacePullDownView.selectedModel.value
//        || [_workPlacePullDownView.selectedModel.value isEqualToString:@""]) {
//        
//        NSString *errMsg = [self getErrMsgWith:@"現場名" itemType:HOTInputItemPullDown];
//        
//        ZZZPopupView *popupView = [[ZZZPopupView alloc] initWithTitle:ALERT_TITLE message:errMsg delegate:nil cancelButtonTitle:nil otherButtonTitle:ALERT_BTN_OK];
//        
//        [popupView showInWindow];
//        return;
//    }
    
    HOTDumpSearchResultViewController *srvc = [[HOTDumpSearchResultViewController alloc]
                                               initWithNibName:@"HOTDumpSearchResultViewController"
                                               bundle:nil
                                               dist_id:self.dist_id
                                               company_id:self.company_id
                                               workp_id:self.workp_id
                                               free_word:[HOTUtils getString:_freeWordText.text]];
    srvc.workpId = _workPlacePullDownView.selectedModel.value;
    [self.navigationController pushViewController:srvc animated:YES];
}
- (IBAction)addSpaceNameBtnClicked:(id)sender {
    
    HOTAddGenbaViewController *asvc = [[HOTAddGenbaViewController alloc] initWithNibName:@"HOTAddGenbaViewController" bundle:nil];
    
    [self.navigationController pushViewController:asvc animated:YES];
}

-(NSString *)workp_id {
    
    if (_workp_id ==nil) {
        
        return @"";
    }
    
    return _workp_id;
}

-(NSString *)dist_id {
    
    if (_dist_id ==nil) {
        
        return @"";
    }
    
    return _dist_id;
}

-(NSString *)company_id {
    
    if (_company_id ==nil) {
        
        return @"";
    }
    
    return _company_id;
}

-(NSString *)free_word {
    
    if (_free_word ==nil) {
        
        return @"";
    }
    
    return _free_word;
}

@end
